package com.xinbochuang.machine.admin.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinbochuang.machine.admin.domain.Camera;
import com.xinbochuang.machine.admin.mapper.CameraMapper;
import com.xinbochuang.machine.admin.service.ICameraService;
import com.xinbochuang.machine.admin.service.system.ISysDeptService;
import com.xinbochuang.machine.common.utils.ServletUtils;
import com.xinbochuang.machine.framework.web.service.TokenService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 *
 * @author 杜文博
 * @date 2020-10-28
 */
@Service
public class CameraServiceImpl extends ServiceImpl<CameraMapper, Camera> implements ICameraService {

    @Resource
    CameraMapper cameraMapper;

    @Resource
    TokenService tokenService;

    @Resource
    private ISysDeptService deptService;

    @Override
    public IPage<Camera> selectList(Camera camera) {
        List<Integer> deptIds = deptService.getDeptIdsByDataScope();
        return cameraMapper.selectCameraList(new Page<>(camera.getPageNum(), camera.getPageSize()), camera, deptIds);
    }

    @Override
    public boolean insertCermera(Camera camera) {
        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
        camera.setUuid(uuid);
        camera.setCreateBy(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getNickname());
        camera.setUpdateBy(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getNickname());
        camera.setCreateTime(DateUtil.now());
        camera.setUpdateTime(DateUtil.now());
        camera.setDeleteFlag(0);
        return save(camera);
    }

    @Override
    public boolean updateCamera(Camera camera) {
        camera.setUpdateTime(DateUtil.now());
        camera.setUpdateBy(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getNickname());
        return updateById(camera);
    }

    @Override
    public boolean deleteCamera(String uuids) {
        Camera camera = cameraMapper.selectById(uuids);
        camera.setDeleteFlag(1);
        camera.setUpdateBy(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getNickname());
        camera.setUpdateTime(DateUtil.now());
        return updateById(camera);
    }

    @Override
    public boolean cameraUser(Camera camera) {
        Camera result = cameraMapper.selectById(camera.getUuid());
        result.setUserType(camera.getUserType());
        result.setUpdateBy(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getNickname());
        result.setUpdateTime(DateUtil.now());
        return updateById(result);
    }

    @Override
    public boolean cameraWarning(Camera camera) {
        Camera result = cameraMapper.selectById(camera.getUuid());
        result.setUserType(camera.getWarningType());
        result.setUpdateBy(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getNickname());
        result.setUpdateTime(DateUtil.now());
        return updateById(result);
    }

    @Override
    public Camera getDetail(String uuid) {
        return cameraMapper.selectById(uuid);
    }
}
